#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <cstdlib>
#include <stdlib.h>

#include <mxml.h>

#ifndef ORGANISER_RULES_PATH
#define ORGANISER_RULES_PATH "xml/organiser_rules.xml"
#endif

#ifndef RULEIO_H
#define RULEIO_H

#include "rule.h"
#include "vars.h"
#include "debug.h"
#include "ruleio.h"

#define CONDITION_TAG "c"
#define CONDITIONS_TAG "conditions"
#define RULE_TAG "r"
#define RULEGROUP_TAG "rulegroup"
#define RULEGROUPS_TAG "rulegroups"
#define SEARCH_TAG "search"
#define IN_TAG "in"

#define ENABLED_ATTR "e"
#define ID_ATTR "id"
#define NAME_ATTR "name"
#define ACTION_ATTR "a"
#define TO_PATH_ATTR "to_path"
#define COND_TYPE_ATTR "t"
#define SEARCH_TYPE_ATTR "t"
#define COND_OP_ATTR "o"
#define DISCOGS_ATTR "discogs"

#define COND_TYPE_COND "condition"
#define COND_TYPE_BOOL "bool"
#define TRUE_VAL 't'

using namespace Org;
using namespace std;

namespace Org
{
    class RuleIO
    {
        friend class Organiser;

        public:
                RuleIO();
                ~RuleIO();

                static vector<Rule> read_xml(const string &filename);
                static vector<RuleGroup> parse_rulegroup_xml(const string &xml);
                static void write_xml(const vector<Rule> rules,
                                                const string &filename);

                static string get_map_action_key(int val);
        private:
                Rule* get_rules();
                static void add_conditions(Node node, mxml_node_t *parent);
                static void extract_conditions(Node *node, mxml_node_t *parent);
                static string parse_text_node(mxml_node_t *text_node);
     };
}

#endif // RULEIO_H
